#ifndef __COMMON_H__
#define __COMMON_H__

#ifndef _MSC_VER
typedef unsigned char       UINT8;
typedef unsigned short      UINT16;
typedef unsigned int        UINT32;
typedef unsigned long long  UINT64;
typedef int BOOL;
#endif

typedef int         STATUS;
typedef int         arg_t;
#define OK          (0)
#define ERROR       (-1)
#define FALSE       (0)
#define TRUE        (1)


#if   defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
#define GEAR_API __declspec(dllexport)
#else
#define GEAR_API __attribute__((visibility("default")))
#endif

/**
 * Variable-argument unused annotation
 */
#define UNUSED(e, ...)      (void) ((void) (e), ##__VA_ARGS__)

#ifdef __GNUC__
#define LIKELY(x)           (__builtin_expect(!!(x), 1))
#define UNLIKELY(x)         (__builtin_expect(!!(x), 0))
#else
#define LIKELY(x)           (x)
#define UNLIKELY(x)         (x)
#endif

#define SWAP(a, b)          \
    do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)

#define MIN(a, b)          ((a) > (b) ? (b) : (a))
#define MAX(a, b)          ((a) > (b) ? (a) : (b))
#define ABS(x)             ((x) >= 0 ? (x) : -(x))

#define CALLOC(size, type)  (type *) calloc(size, sizeof(type))
#define ARRAY_SIZE(a)       (sizeof(a) / sizeof(a[0]))

#define VERBOSE()                                                   \
    do {                                                            \
        printf("%s:%s:%d xxxxxx\n", __FILE__, __func__, __LINE__);  \
    } while (0)

#define DUMP_BUFFER(buf, len)                                             \
    do                                                                    \
    {                                                                     \
        int _i, _j = 0;                                                   \
        char _tmp[128] = {0};                                             \
        if (buf == NULL || len <= 0)                                      \
        {                                                                 \
            break;                                                        \
        }                                                                 \
        for (_i = 0; _i < len; _i++)                                      \
        {                                                                 \
            if (!(_i % 16))                                               \
            {                                                             \
                if (_i != 0)                                              \
                {                                                         \
                    printf("%s", _tmp);                                   \
                }                                                         \
                memset(_tmp, 0, sizeof(_tmp));                            \
                _j = 0;                                                   \
                _j += snprintf(_tmp + _j, 64, "\n%p: ", buf + _i);        \
            }                                                             \
            _j += snprintf(_tmp + _j, 4, "%02hhx ", *((char *)buf + _i)); \
        }                                                                 \
        printf("%s\n", _tmp);                                             \
    } while (0)

#define ALIGN2(x, a)	(((x) + (a) - 1) & ~((a) - 1))

#define is_alpha(c)  (((c) >=  'a' && (c) <= 'z') || ((c) >=  'A' && (c) <= 'Z'))

/**
 * Compile-time strlen(3)
 * XXX: Should only used for `char[]'  NOT `char *'
 * Assume string ends with null byte('\0')
 */
#define STRLEN(s)          (sizeof(s) - 1)

#endif